<?php

class AdviceContentController extends Controller
{

    public $defaultAction = 'manage';


    public function actionCreate()
    {
        $model = new Advice();
        $model->type = Advice::ADVICE_TYPE_CONTENT;
        if (isset($_POST['Advice'])) {
            $model->attributes = $_POST['Advice'];
            if ($model->save()) {
                Yii::app()->user->setFlash('alert-success', 'Create Success');
                $this->redirect($this->defaultAction);
            } else {
                Yii::app()->user->setFlash('alert-failure', 'Create failed');
            }
        }
        $this->render('model', array(
            'model' => $model,
        ));
    }

    public function actionUpdate()
    {
        if (isset($_GET['id'])) {
            $model = Advice::model()->findByPk($_GET['id']);
            if ($model) {
                if (isset($_POST['Advice']) && ($model->attributes = $_POST['Advice'])) {
                    if ($model->update()) {
                        Yii::app()->user->setFlash('alert-success', 'Update Success');
                        $this->redirect(array($this->defaultAction));
                    } else {
                        if ($model->hasErrors('status')) {
                            Yii::app()->user->setFlash('alert-failure', 'Update failure , the config can\'t work  and  error is ' . $model->getError('status'));
                        }
                    }
                }
                $this->render('model', array(
                    'model' => $model,
                ));
                return;
            }
        }
        Yii::app()->user->setFlash('alert-failure', "Update failure");
        $this->redirect(array($this->defaultAction));    }

    public function actionDelete()
    {
        if (Yii::app()->request->isAjaxRequest) {
            $result = 'Success';
            $id = $this->getQuery('get', 'id', null);
            $model = Advice::model()->with('advice_exts')->findByPk($id);
            if ($model) {
                if ($model->delete()) {
                    $result = 'Success';
                } else {
                    $result = 'delete failure';
                }
            }
            echo $result;
        } else {
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
        }
    }

    public function actionIndex()
    {
        $this->render('index');
    }

    public function actionManage()
    {
        session_write_close();
        $model = new Advice('search');
        $model->type = Advice::ADVICE_TYPE_CONTENT;
        if (isset($_GET['Advice'])) {
            $model->attributes = $_GET['Advice'];
        }
        if (isset($_GET['searchKey'])) {
            $model->searchKey = $_GET['searchKey'];
        }
        $this->render('manage', array(
            'model' => $model,
        ));
    }

    public function actionLink()
    {
        if (isset($_GET['id'])) {
            $model = Advice::model()->findByPk($_GET['id']);
            if (Yii::app()->getRequest()->isPostRequest) {
                $model->extids = isset($_POST['Advice']['extids'])?$_POST['Advice']['extids']:[];
                if ($model->update()) {
                    Yii::app()->user->setFlash('alert-success', 'Link Success');
                    $this->redirect(array($this->defaultAction));
                } else {
                    if ($model->hasErrors('status')) {
                        Yii::app()->user->setFlash('alert-failure', 'Link failure , the config can\'t work  and  error is ' . $model->getError('status'));
                    }
                }
            }
            $this->render('link', array(
                'model' => $model,
            ));
            return;
        }
        Yii::app()->user->setFlash('alert-failure', "Link failure");
        $this->redirect(array($this->defaultAction));
    }

    public function actionUpload()
    {
        $id = $this->getQuery('get', 'id', null);
        $model = Advice::model()->findByPk($id);
        if ($model) {
            if ($model->upload()) {
                Yii::app()->user->setFlash('alert-success', "upload Success");
            } else {
                Yii::app()->user->setFlash('alert-failure', 'upload failure');
            }
        } else {
            Yii::app()->user->setFlash('alert-failure', 'upload failure');
        }
        $this->redirect($this->defaultAction);
    }

    public function colRevise($data, $row, $c)
    {
        $name = $c->name;
        switch ($name) {
            case 'adviceContent':
                $html = '';
                $topics = $data->advice_exts;
                $html .= sizeof($topics);
                return $html;
                break;
            case 'upload':
                $html = '';
                $html .= ($data->upload == 1 ? '<p style="color: green">上线</p>' : '已下线');
                return $html;
                break;
            case 'operations':
                $params = [];
                $res = '';
                //edit
                $params[] = Yii::app()->getController()->createUrl("update", array("id" => $data->id,));
                $res .= '<a class ="" title="编辑" href="%s"> 编辑</a>';
                //switch
                $params[] = Yii::app()->getController()->createUrl("upload", array("id" => $data->id,));
                $params[] = $data->isUpload() ? "下线" : "上线";
                $res .= '<a class ="upload "  href="%s"> %s</a>';
                //delete
                $params[] = Yii::app()->getController()->createUrl("delete", array("id" => $data->id,));
                $res .= '<a class ="delete "  tooltip="delete" href="%s"> 删除</a>';
                $params[] = Yii::app()->getController()->createUrl("link", array("id" => $data->id,));
                $res .= '<a class="" aria-hidden="true" href="%s">推荐内容列表</a>';
                return vsprintf($res, $params);
                break;
            default:
                return $data->$name;
        }
    }

}